﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using NetworkCommsDotNet;
using System.Threading;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Microsoft.Phone.Scheduler;
using Microsoft.Phone.Notification;
using System.Text;

using Jive.CustomControls;
using ProtoBuf;
using System.IO.IsolatedStorage;
using System.IO;
using System.Runtime.Serialization;
using System.Collections.ObjectModel;
using System.Windows.Resources;
using System.Windows.Media.Animation;
using Jive.ViewModels;
using System.Windows.Data;
using GalaSoft.MvvmLight.Messaging;
using Jive.Settings;


namespace Jive
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
            Messenger.Default.Register<string>(this, ShowMessage);
            Messenger.Default.Register<NotificationMessageAction<MessageBoxResult>>(this, (m) =>
            {
                if (m.Notification == "QueryServerDeleteDialog")
                {
                    var result = MessageBox.Show("Are you sure?", string.Format("Delete {0}", AppSettings.AppSettingsInstance.ServerSetting), MessageBoxButton.OKCancel);
                    m.Execute(result);
                }
            });

            //ProgressIndicator prog = new ProgressIndicator();
            //prog.IsIndeterminate = false;
            //prog.Text = "Jive Messenger";
            //prog.IsVisible = true;
            //SystemTray.SetProgressIndicator(this, prog);
        }

        /// <summary>
        /// Shows the message.
        /// </summary>
        /// <param name="message">The message.</param>
        private void ShowMessage(string message)
        {
            this.Dispatcher.BeginInvoke(() => MessageBox.Show(message));
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (this.JivePivot.SelectedItem == GroupChatPivot)
            {
                MessageBoxResult mb = MessageBox.Show("Exit Jive Messenger?", "Exit", MessageBoxButton.OKCancel);
                if (mb != MessageBoxResult.OK)
                    e.Cancel = true;
            }
            else if (this.JivePivot.SelectedItem == ContactsPivot)
            {
                ((ViewModelMainPage)this.DataContext).SelectedPivotItemIndex = 0;
                e.Cancel = true;
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            
            this.GroupChatPivot.Visibility = System.Windows.Visibility.Visible;
            if (e.NavigationMode != NavigationMode.New)
            {
                if (this.MainPageEmoticonPopup.IsOpen == false)
                    this.MainPageEmoticonPopup.IsOpen = true;
                if (EmoticonControl.NeedRefresh)
                {
                    ViewModelMainPage vmmp = (ViewModelMainPage)this.DataContext;
                    vmmp.CreateCustomEmoticons();
                    //vmmp.CustomEmoticons = vmmp.CreateCustomEmoticonList();
                    EmoticonControl.NeedRefresh = false;
                }
            }
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            this.GroupChatPivot.Visibility = System.Windows.Visibility.Collapsed;
            this.MainPageEmoticonPopup.IsOpen = false;
            base.OnNavigatedFrom(e);
        }

        private void FauxKeyboardControl_Shown(object sender, EventArgs e)
        {
            this.JivePivot.IsLocked = true;
        }
        private void FauxKeyboardControl_hidden(object sender, EventArgs e)
        {
            this.JivePivot.IsLocked = false;
        }
    }
}